# -*- coding:utf-8 -*-
import os
import re
import openpyxl as opxl
import pysvn
import datetime
import getopt
import urllib
import logging
from logging import handlers
log = None 
import sys, getopt

localRevision = None
localPath = "../../resource"
svnurl= 'svn://svn.taole.com/pisces_res/branches/aquarius_taiwan_trunk/'

class Logger(object):
    level_relations = {
        'debug':logging.DEBUG,
        'info':logging.INFO,
        'warning':logging.WARNING,
        'error':logging.ERROR,
        'crit':logging.CRITICAL
    }#日志级别关系映射

    def __init__(self,filename,level='info',when='D',backCount=3,fmt='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'):
        self.logger = logging.getLogger(filename)
        format_str = logging.Formatter(fmt)#设置日志格式
        self.logger.setLevel(self.level_relations.get(level))#设置日志级别
        sh = logging.StreamHandler()#往屏幕上输出
        sh.setFormatter(format_str) #设置屏幕上显示的格式
        th = handlers.TimedRotatingFileHandler(filename=filename,when=when,backupCount=backCount,encoding='utf-8')#往文件里写入#指定间隔时间自动生成文件的处理器
        #实例化TimedRotatingFileHandler
        #interval是时间间隔，backupCount是备份文件的个数，如果超过这个个数，就会自动删除，when是间隔的时间单位，单位有以下几种：
        # S 秒
        # M 分
        # H 小时、
        # D 天、
        # W 每星期（interval==0时代表星期一）
        # midnight 每天凌晨
        th.setFormatter(format_str)#设置文件里写入的格式
        self.logger.addHandler(sh) #把对象加到logger里
        self.logger.addHandler(th)

def main(argv):
    global log
    global localPath
    inputfile = ''
    try:
        opts, args = getopt.getopt(argv,"hi:o:",["ifile="])
    except getopt.GetoptError:
        print('test.py -i <inputfile>')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print ('test.py -i <inputfile> -o <outputfile>')
            sys.exit()
        elif opt in ("-i", "--ifile"):
            inputfile = arg
    localPath = inputfile
    log.logger.info('提取的文件目录为：{}'.format(inputfile))

#回调函数，获取登录到svn仓库的用户名和密码 
def get_login( realm, user, may_save ):
    #svn账号
    username = "wangwanjue"
    #svn密码
    password = ""
    return True, username, password, False

def get_python_paths(path='./Files'):
    """
    获取LuaFiles文件夹中所有以.lua||.py为后缀的文件路径
    :param path: 当前传入文件夹路径
    :return: None
    """
    for l in os.listdir(path):
        current_path = path + '/' + l
        if os.path.isfile(current_path):                            # 如果路径为文件，则判断是否以.lua结尾，是的话则放入列表中
            if current_path.endswith('.py') and not current_path.endswith('/badword.py'):
                ALL_PYTHON_PATHS.append(current_path)
        else:                                                       # 如果是文件夹，则递归
            get_python_paths(current_path)


def manage_python_files(python_path,excel_chinese_list,add_chinese_list):
    """
        打开lua文件
        :param python_path: lua文件路径
        :return: None
        """
    chinese_list={}
    name = re.findall(r'[^\\/:*?"<>|\r\n]+$',python_path)
    print('该路径{}脚本开始处理>>>>>'.format(python_path))                  # 打印进度
    with open(python_path, 'r') as f:
        chinese_list = get_chinese(f.readlines())
   
    for e ,value in chinese_list.items():
        key = replace_richText(e)
        print('处理>>>>>{}-----{}'.format(OnlyStr(key),value[0]))
        if OnlyStr(key) not in excel_chinese_list:
            if OnlyStr(key) not in add_chinese_list:
                add_chinese_list[OnlyStr(key)]=['{}_{}'.format(name[0],value[0]),key]
    print('该路径{}脚本已经处理完毕'.format(python_path))                  # 打印进度
	
def get_chineseNLines(content,chinese_list,needIndex):
    """
    获取所有中文字符串
    :param content: lua文件内容
    :return: 所有符合的中文字符串
    """
    all_result1 = re.findall(r'\[\[(?:.|\r|\n)*?\]\]', content)                        # 首先提取所有双引号内的字符
    for a in all_result1:
        if re.search(r'[\u4e00-\u9fa5]+', a):                        # 判断双引号内是否有中文
            if a.count('[[') > 1:                                     # 此处判断为排除"ComposePage", "合成产物无效"这种情况
                judge = a.split('[[')                                 # 将上述情况的字符串根据"分割并再做判断
                for j in judge:
                    if re.search(r'[\u4e00-\u9fa5]+', j):
                        b=j.replace('[[', '')
                        c=b.replace(']]', '')
                        chinese_list.append(c)
            else:
                b=a.replace('[[', '')
                c=b.replace(']]', '')
                chinese_list.append(c)
	
    return chinese_list

def IF_ELSE(bStatement ,X,Y):
    if(bStatement):
        return X
    else:
        return Y

def get_LineAllConnectNew(content,needTextList):
    print("------->>>>{}".format(content))
    isYinghaonei = False
    isBegin = True
    dianCount = 0
    word = ""  
    words =[]
    newords = {} 
    index = 0
    find = 0
    isNeedAdd = 0
    for iword in content:
        if((iword=='"'or iword=="'") and isBegin == True):
            isNeedAdd = 1
            isYinghaonei = IF_ELSE(isYinghaonei == True, False, True)
        if(isBegin == True  ):
            if(isYinghaonei==False and iword =="+"):
                dianCount=dianCount+1
                if(dianCount==1):
                    dianCount =0
                    isNeedAdd = 1
                    find = 1
            else:
                # if(dianCount==1):
                #     word = word + "+"
                #     word = word + iword
                #     dianCount = 0
                # else:
                if (isYinghaonei == False and iword ==" " ):
                    word = word
                else:
                    if(isNeedAdd == 1 and isYinghaonei==True):
                        word = word
                    else:    
                        word = word + iword              
        if((iword =="\n" or isNeedAdd == 1) and isBegin == True ):           
            if (word !=""):
                words.append(word)
            word = ""  
            if(len(words)>0):
                index = index +1
                newords[index] = words
                words=[]
            if(iword =="\n"):
                isBegin = False
            if(isNeedAdd == 1 and isYinghaonei==True):
                word = iword    
            dianCount = 0
            isNeedAdd = 0
            continue 
    chineseKey = ""  
    if(find>0):         
        for i,value in newords.items():
            print("------->>>>{}".format(value)) 
            for iword in value:      
                if(iword.count('"') > 1 or iword.count("'")>1):
                    content = content.replace(iword,"")
                    tem = iword.replace("'","")
                    tem = tem.replace('"',"")
                    chineseKey = chineseKey + tem              
                else:
                    chineseKey = chineseKey +"{}"              
                    content = content.replace(iword,"")
        if(chineseKey!=""):
            needTextList.append(chineseKey)
    return content


def replace_richText(content):
    print(' 富文本处理content是：{}'.format(content))
    allRichText = re.findall(r'#A|#R|#D&|#H&|#r|%s|%d|@N|\$SRV|\$P|\$I|\$N|\$O|\$M|\$p1|\$p2|\$FROM|\$TO|%:m|%:g|%:w|%:s|%:t|!:m|!:g|!:w|!:s|!:t|\$[GW][0-9]+|#E[0-9]+&|#F[0-9]+&|#C[0-9]+&|#[0-9]+|#P[0-9]+&', content)
    print(' 富文本处理allRichText是：{}'.format(allRichText))
    index = 0
    for a in allRichText:
        index = index+1
        print(' 富文本处理{}'.format(a))
        content = content.replace(a,"<{}>".format(index))
    # allRichText = re.findall(r'\$.+\$', content)
    # print(' 富文本处理allRichText是：{}'.format(allRichText))
    # for a in allRichText:
    #     index = index+1
    #     print(' 富文本处理{}'.format(a))
    #     content = content.replace(a,"<{}>".format(index))
    return content

def get_findFormat(content):
    content = re.sub(r'(?:print).*[\'\"].*[\'\"].*[\)]','', content)
    all_result1 = re.findall(r'(?:=)(.*[\+].+){1}(?=[\,|\;|\n])', content)
    return content
    
def IsPassLine(strLine):
    # 是否是可以忽略的行
    # 可忽略行的正则表达式列表
    RegularExpressions = ["""\'.*#.*\'""", """\".*#.*\"""","""\'\'\'.*#.*\'\'\'""", """\"\"\".*#.*\"\"\""""]
    for One in RegularExpressions:
        zz = re.compile(One)
        if re.search(zz, strLine) == None:
            continue
        else:
            return True  # 有匹配 则忽略
    return False

def get_findzhushi(content):
    index = content.find('#')  # 获取带注释句‘#’的位置索引
    NewStr = ""
    if index == -1  or IsPassLine(content):
        if content.strip() != '':  # 排除纯空的行
            NewStr =  content
    else:
        NewStr = content
        if index != 0:
            NewStr = content[:index] + '/n'  # 截取后面的注释部分
    return NewStr

def get_chineseOneLine(content,chinese_list,needIndex):
    """   
    """
    content = get_findzhushi(content)
    content = re.sub(r'(?:print).*[\'\"].*[\'\"].*[\)]','', content)  
    content = content.replace('\\"','<T>')     
    all_result1 = re.findall(r'(?:=)(.*[\+].+){1}(?=[\,|\;|\n])', content)              #
    needTextList = []
    if(len(all_result1)):
        content = get_LineAllConnectNew(content,needTextList)
        for a in needTextList:
            if re.search(r'[\u4e00-\u9fa5]+', a) :                    
                chinese_list[a]=[needIndex,a]
    needTextList = []
    all_result2 = re.findall(r'(?:\()(.*[\+].+){1}(?=[,|\)])', content)
    if(len(all_result2)):
        content = get_LineAllConnectNew(content,needTextList)
        for a in needTextList:
            if re.search(r'[\u4e00-\u9fa5]+', a) :                    
                chinese_list[a]=[needIndex,a]
    get_chineseOne_(content,chinese_list,needIndex)
    return chinese_list

def get_chineseOne_(content,chinese_list,needIndex):
    all_result = re.findall(r'[\'\"](.*?)[\'\"]', content)              # 
    for a in all_result:
        if re.search(r'[\u4e00-\u9fa5]+', a) :                        # 
            if a.count('"') > 2:                                     # 
                judge = a.split('"')                                 # 
                for j in judge:
                    j=j.replace('"',"")
                    if re.search(r'[\u4e00-\u9fa5]+', j):
                        chinese_list[j]=[needIndex,j]
            elif a.count("'") > 2:                                  # 
                judge = a.split("'")                                 # 
                for j in judge:
                    j=j.replace("'","")
                    if re.search(r'[\u4e00-\u9fa5]+', j):
                        chinese_list[j]=[needIndex,j]
            else:
                a=a.replace("'","")
                a=a.replace('"',"")
                chinese_list[a]=[needIndex,a]
    return chinese_list
def get_chinese(content):
    """
    获取所有中文字符串
    :param content: lua文件内容
    :return: 所有符合的中文字符串
    """
    addTextlines = ""
    addLine = False
    chinese_one_list = {}
    index = 0 
    needIndex =0 	
    for line in content:
        index = index+1
        lineTtpe = CheckSingleLine(OnlyStr(line),addLine)
        if(lineTtpe==1): # 注释不需要提取
            addTextlines = ""
            addLine = False	
        elif(lineTtpe==2):# ]]结束需要提取		
            addLine = False
            addTextlines += line
            #get_chineseNLines(addTextlines,chinese_one_list,needIndex)
            addTextlines = ""
        elif(lineTtpe==3):# [[开始需要相加行
            needIndex = index
            addLine = True
            addTextlines += line         	
        elif(lineTtpe==4):# “”‘’需要提取
            addTextlines = ""
            addLine = False	
            needIndex = index
            get_chineseOneLine(line,chinese_one_list,needIndex)										
        else:
            addTextlines = ""
            addLine = False						
    return chinese_one_list

def OnlyStr(tmp_str):
    if(tmp_str == None):
        return None
    return tmp_str.strip("\r\n").strip("\n").strip("\t").strip()

#检查单独一行
def CheckSingleLine(str_line,isAddLine):
    if(isAddLine):
        zh_match = re.match(r'.*[\"\"\"|\'\'\'].*', str_line)
        if zh_match:
            return 2
        return 3
    else:	
        if "#" == str_line[:1]:
            return 1
        else:
            zh_match = re.search(r'\"\"\"|\'\'\'', str_line)
            if(zh_match):
                zh_match = re.search(r'\"\"\"|\'\'\'', str_line)
                if(zh_match):
                    return 2    
                return 3			
            zh_match = re.search(r'.*[\'\"].*[\u4e00-\u9fa5]+.*[\'\"].*', str_line)
            if(zh_match):
                return 4
            return 5

def get_excel_chinese():
    """
    获取excel表中第一列的全部内容，用于后续比较是否重复
    :return: excel文件第一列的所有文本
    """
    global log
    excel_chinese_list = {}
    pathName = 'translation.xlsx'
    pathName2 = 'translationCompleted.xlsx'
    fail_list = []
    wb =None
    wb2 =None
    if(os.path.isfile(pathName2)):
        wb2 = opxl.load_workbook(pathName2)
        ws = wb2.active
        ws = wb2['content']
        ws3 = wb2['revisionSever']
        ws4 = wb2['replaceRevisionSever']
        # ws4 = wb2['updateContent']
        ws6=wb2['failFillsSever']
    else:
        wb2 = opxl.Workbook()
        ws = wb2.active
        ws.title = 'content'
        ws3 = wb2.create_sheet('revision')
        ws4 = wb2.create_sheet('replaceRevision')
        wb2.create_sheet('revisionSever')
        wb2.create_sheet('replaceRevisionSever')
        wb2.create_sheet('failFills')
        wb2.create_sheet('failFillsSever')
        wb2.save(pathName2)
        ws6=wb2['failFillsSever']
    if(os.path.isfile(pathName)):
        wb = opxl.load_workbook(pathName)
        ws2 = wb.active
        ws2 = wb['updateContent']
    else:
        wb = opxl.Workbook()
        ws2 = wb.active
        ws2.title = 'updateContent'
        wb.save(pathName)
    for rx in range(2, ws.max_row + 1):
        temp_list = []
        w1 = ws.cell(row=rx, column=1).value      
        w2 = ws.cell(row=rx, column=2).value
        w3 = ws.cell(row=rx, column=3).value
        print('开始读入content>>>>>{}-----{}------{}--{}'.format(rx,w1,w2,w3))
        temp_list = [w1,w2,w3]
        excel_chinese_list[OnlyStr(w2)] = temp_list
    log.logger.info("读入content完成")
    for rx in range(2, ws2.max_row + 1):
        temp_list = []
        w1 = ws2.cell(row=rx, column=1).value      
        w2 = ws2.cell(row=rx, column=2).value
        w3 = ws2.cell(row=rx, column=3).value
        print('开始读入updateContent>>>>>{}-----{}------{}--{}'.format(rx,w1,w2,w3))
        temp_list = [w1,w2,w3]
        if OnlyStr(w2) not in excel_chinese_list:
            excel_chinese_list[OnlyStr(w2)] = temp_list
    log.logger.info("读入updateContent完成")
    for rx in range(1, ws6.max_row+1):
        w1 = ws6.cell(row=rx, column=1).value      
        fail_list.append(w1)
    revision = ws3.cell(row=1, column=1).value
    return [excel_chinese_list,revision,fail_list]


def write_excel(chinese_list):
    """
    将内容写到translation.xlsx中 
    :param chinese_list: 要写的中文字符串列表
    :return: None
    """
    global log
    pathName = 'translation.xlsx'
    pathName2 = 'translationCompleted.xlsx'
    wb = opxl.load_workbook(pathName)
    wb2 = opxl.load_workbook(pathName2)
    ws = wb['updateContent']
    # ws2 = wb2['updateContent']
    ws3 = wb2['revisionSever']
    ws.insert_rows(2, len(chinese_list))                                # 在开头插入一些空行用于写入
    log.logger.info('共处理行数>>>>>{}'.format(len(chinese_list)))
    ws["A1"] = "local language file name"
    ws["B1"] = "simplified Chinese"
    ws["C1"] = "traditional Chinese"
    index = 2
    for i, value in chinese_list.items():
        print('开始写入>>>>>{}-----{}------{}'.format(value,i,len(chinese_list.items())))
        cell_num1 = 'A{}'.format(index)
        cell_num2 = 'B{}'.format(index)
        ws[cell_num1] = value[0]
        ws[cell_num2] = value[1]
        index += 1
    log.logger.info('写入完成>>>>>')
    cell_num1 = 'A{}'.format(1)
    ws3[cell_num1] = localRevision
    wb.save(pathName)
    wb2.save(pathName2)
def checkinDiff(revision,filleList):
    global log
    client = pysvn.Client()
    #登陆到svn
    client.callback_get_login = get_login
    log.logger.info(filleList)

    # checkAdd(filleList)
    # changes = client.status('./examples/pysvn')
    # for f in changes:
    #     if f.text_status == pysvn.wc_status_kind.added:
    #         client.add(localPath+)
    # client.checkin(filleList, 'by translation code')

def checkAdd(filles):
    global log
    client = pysvn.Client()
    #登陆到svn
    # client.callback_get_login = get_login
    log.logger.info(filles)
    for key in filles:
        info = client.info(key)
        log.logger.info(key)
        if info == None:
            client.add(key)

def update(client):
    global log
    try:        
        revision = None
        try:
            log.logger.info("更新中1{}".format(localPath))
            revision = client.update(localPath)
            log.logger.info("更新中1{}".format(revision))
            return True
        except:
            revision = None
            log.logger.info("更新中3{}".format(revision))
        if not os.path.exists(os.path.join(localPath, ".svn")):
            revision = None
            log.logger.info("更新中4{}".format(revision))
        if revision is None or revision[0].number == -1:
            log.logger.info("更新中2{}".format(revision[0].number))
            client.checkout(svnurl, localPath)
            return True
    except:
        log.logger.error("error")
        return False

def findDiff(revision):
    global log
    # new_diff_list = []
    revision_min = None
    revision_max = None
    old_revision = revision
    new_revision = revision
    client = pysvn.Client()
    #登陆到svn
    client.callback_get_login = get_login
    bUpdate = update(client)
    if bUpdate:
        entry = client.info(localPath)
        new_revision =  entry.commit_revision.number
    #开始的版本号
    if old_revision == None:
        revision_min = pysvn.Revision( pysvn.opt_revision_kind.base )
    else:
        revision_min = pysvn.Revision( pysvn.opt_revision_kind.number, int(old_revision))
    #获取svn目前的版本号
    if new_revision == None:
        revision_max = pysvn.Revision( pysvn.opt_revision_kind.head )
    else:
        revision_max = pysvn.Revision( pysvn.opt_revision_kind.number, int(new_revision))
    
    log.logger.info('最新版本:{}'.format(revision_max))
    #获取svn之前的版本号
    if old_revision != None:
        #获取两个版本之间改动的文件列表
        summary = client.diff_summarize(localPath,revision_min,localPath,revision_max)
        fileListPython = []
        for changed in summary:
            #手动删除掉已经删除但还存留的垃圾文件  
            fullPath = ""
            if pysvn.diff_summarize_kind.delete == changed['summarize_kind']:
                fullPath = localPath+"/"+changed['path'] 
                log.logger.info('删除:{}'.format(fullPath))  
            if os.path.exists(fullPath):
                os.remove(fullPath)
            #获取两个版本之间修改的和新增的文件
            if  pysvn.diff_summarize_kind.added == changed['summarize_kind'] or pysvn.diff_summarize_kind.modified == changed['summarize_kind']:
                log.logger.info(changed['summarize_kind'], changed['path'])
                if changed['node_kind'] == pysvn.node_kind.file:
                    if pysvn.diff_summarize_kind.added == changed['summarize_kind']:
                        fullPath = localPath+"/"+changed['path']
                    else:
                        fullPath = localPath+"/"+changed['path']       
                    if(fullPath.endswith('.py')):
                        log.logger.info(svnurl+"/"+changed['path'])
                        fileListPython.append(fullPath)
    if bUpdate:
        if revision !=None:
            entry = client.info(localPath)
            log.logger.info('SVN路径:{}'.format(entry.url))
            log.logger.info('最新版本:{}'.format(entry.commit_revision.number))
            log.logger.info('提交人员:{}'.format(entry.commit_author))
            log.logger.info('更新日期:{}'.format( datetime.datetime.fromtimestamp(entry.commit_time)))
            revision =  entry.commit_revision.number
            log.logger.info('处理>>>>>1{}'.format([fileListPython,revision]))
            return [fileListPython,revision]
        else:
            entry = client.info(localPath)
            log.logger.info('SVN路径:{}'.format(entry.url))
            log.logger.info('最新版本:{}'.format(entry.commit_revision.number))
            log.logger.info('提交人员:{}'.format(entry.commit_author))
            log.logger.info('更新日期:{}'.format( datetime.datetime.fromtimestamp(entry.commit_time)))
            revision =  entry.commit_revision.number
            get_python_paths(localPath)
            log.logger.info('处理>>>>>2{}'.format([1,revision]))
            return [ALL_PYTHON_PATHS,revision]
    return [ALL_PYTHON_PATHS,revision]


if __name__ == '__main__':
    dirPath = "config_log"
    if not os.path.exists(dirPath):
        os.makedirs(dirPath)
    log = Logger('config_log/get_chinese_log.log',level='debug')
    Logger('config_log/get_chinese_log_error.log', level='error').logger.error('error')
    main(sys.argv[1:])
    ALL_LUA_PATHS = []
    ALL_PYTHON_PATHS = []
    ALL_JSON_PATHS = []
    failFilleList = []
    old_chinese_list = {}#中文key
    new_chinese_list = {}
    info=get_excel_chinese()
    old_chinese_list = info[0]
    localRevision = info[1]
    failFilleList = info[2]
    info = findDiff(localRevision)
    localRevision = info[1]
    ALL_PYTHON_PATHS = info[0]
    for fullPath in failFilleList:
        if(fullPath!= None):
            if(fullPath.endswith('.py') and not fullPath.endswith('/badword.py')):
                ALL_PYTHON_PATHS.append(fullPath)
    for python_path in ALL_PYTHON_PATHS:
        manage_python_files(python_path,old_chinese_list,new_chinese_list)
    write_excel(new_chinese_list)

